package com.success.Controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.success.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
public class PayUserController {

    //RestTemplate一个在客户端得负载均衡
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/pay/user/{id}")
    @ResponseBody
     public User getPay(@PathVariable("id") Long id){
        String url = "http://localhost:2000/user/"+id;
        return restTemplate.getForObject(url,User.class);
    }

    //这是集成了
    @HystrixCommand(fallbackMethod = "getPayFallback")
    @GetMapping("/pay/users/{id}")
    @ResponseBody
    public User getPays(@PathVariable("id") Long id){
        String url = "http://user-server/user/"+id; //user-server表示得是配置文件中得服务名称
        return restTemplate.getForObject(url,User.class);
    }

    //这是拖低方法
    public User getPayFallback(Long id) {
        User user = new User(id, "错误", 555);
        return user;
    }
}
